package com.copa.module.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.copa.domain.ResponseDto;
import com.copa.module.order.domain.dto.OrderListDto;
import com.copa.module.system.domain.entity.ConfigEntity;
import com.copa.module.system.domain.vo.ConfigListVo;
import com.copa.module.system.domain.vo.ConfigUpdateVo;
import com.copa.module.system.mapper.ConfigDao;
import com.copa.module.system.service.ConfigService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author copa
 * @createDate 2021-02-22 10:05
 * @function
 */
@Service
public class ConfigServiceImpl implements ConfigService {

    @Autowired
    private ConfigDao configDao;

    @Override
    public ResponseDto<IPage<ConfigEntity>> list(ConfigListVo configListVo) {
        Page<ConfigEntity> page = new Page<>(configListVo.getPageVo().getCurrent(), configListVo.getPageVo().getSize());
        IPage<ConfigEntity> configEntityList = configDao.findByKeys(page, configListVo);
        return ResponseDto.success(configEntityList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseDto<String> put(ConfigUpdateVo configUpdateVo) {
        ConfigEntity configEntity = new ConfigEntity();
        BeanUtils.copyProperties(configUpdateVo, configEntity);
        int update = configDao.updateById(configEntity);
        if (update > 0) {
            return ResponseDto.successMsg("修改配置成功！");
        }
        return ResponseDto.errorMsg("系统繁忙，请稍后再试！");
    }
}
